import 'package:baselib/utils/image_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/time_util.dart';
import 'package:baselib/widgets/comm_app_bar.dart';
import 'package:baselib/widgets/inkwell_button.dart';
import 'package:baselib/widgets/refresh_util.dart';
import 'package:flutter/material.dart';
import 'package:framelib/utils/color_util.dart';
import 'package:framelib/utils/drawable_util.dart';
import 'package:framelib/utils/px_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:framelib/widgets/comm_widget_manager.dart';
import 'package:framelib/widgets/page_widget.dart';
import 'package:get/get.dart';
import 'package:module_business/module_chat/viewmodel/chat_view_model.dart';
import 'package:module_business/module_chat/viewmodel/message_view_model.dart';
import 'package:module_business/module_comm/model/user_entity.dart';
import 'package:module_business/module_index/viewmodel/index_view_model.dart';
import 'package:nim_core/nim_core.dart';
/**
 * 消息主页
 */
class MessagePage extends GetWidget<MessageViewModel> {
  @override
  Widget build(BuildContext context) {
    Get.put(MessageViewModel());
    return PageWidget.build(
        builder: (MessageViewModel model){
          return PageWidget.PageScaffoldWidget(
            isShowBg: true,
            appBar: CommAppBar(
              backgroundColor: ColorUtil.instance.color_transparent,
              leadingWidth: PxUtil.instance.screenWidth,
              leading: Container(
                  alignment: Alignment.centerLeft,
                  margin: EdgeInsets.only(
                    left: PxUtil.instance.px_15,
                  ),
                  child: Text(
                    StringUtil.instance.Messages,
                    style: TextStyle(
                        fontSize: PxUtil.instance.px_24,
                        fontWeight: FontWeight.bold
                    ),
                  )
              ),
            ),
            body: RefreshUtil.init(
              model.refreshController,
                  ()=>model.onRefresh(pageCur: model?.page=1),
                  ()=>model.onRefresh(pageCur: model?.page+1),
              _buildListUi(model),
              physics: ClampingScrollPhysics(),
              loadStatus: model?.loadStatus,
              enablePullDown: true,
              enablePullUp: false,
              isCanMoreData: model?.isCanMoreData??true,
              onReFreshFunction:()=>model.onRefresh(pageCur: model?.page=1),
            )
          );
        }
    );
  }

  Widget _buildListUi(MessageViewModel model) {
    var tipLists = (model.dataLists??[]).where((element) => ((element?.lastMessageType == NIMMessageType.tip)||
        (element?.lastMessageType == NIMMessageType.notification)
    )).toList();
    NIMSession tipListItem = null;
    if(tipLists.isNotEmpty)tipListItem = tipLists[0];
    LogUtil.d('tipListItem--->dataLists=${(model.dataLists??[])?.map((e) => e?.toMap()).toList()}');
    LogUtil.d('tipListItem--->tipLists=${(tipLists)?.map((e) => e?.toMap()).toList()}');
    LogUtil.d('tipListItem--->tipListItem=${tipListItem?.toMap()}');
    return ListView(
      shrinkWrap: true,
      physics: NeverScrollableScrollPhysics(),
      padding: EdgeInsets.only(
        left: PxUtil.instance.px_15,
        right: PxUtil.instance.px_15,
        top: PxUtil.instance.px_15,
      ),
      children: [
        //enter my room
        InkWellButton(
          onPressed: ()=>model.voiceRoomAdd(),
          child: Container(
            padding: EdgeInsets.only(
              top: PxUtil.instance.px_8,
              bottom: PxUtil.instance.px_8,
            ),
            child: Row(
              children: [
                ImagesUtil.loadAssetImage(
                    DrawableUtil.instance.icon_msg_my_room,
                    width: PxUtil.instance.px_60
                ),
                SizedBox(width: PxUtil.instance.px_10,),
                Expanded(child: Text(
                  StringUtil.instance.Enter_my_room,
                  style: TextStyle(
                      color: ColorUtil.instance.color_white,
                      fontWeight: FontWeight.w500,
                      fontSize: PxUtil.instance.px_19
                  ),
                ))
              ],
            ),
          ),
        ),
        //system notion
        InkWellButton(
          onPressed: ()=>model.toSystemNotification(tipListItem,null),
          child: Container(
            padding: EdgeInsets.only(
              top: PxUtil.instance.px_8,
              bottom: PxUtil.instance.px_8,
            ),
            child: Row(
              children: [
                ImagesUtil.loadAssetImage(
                    DrawableUtil.instance.icon_msg_system_notification,
                    width: PxUtil.instance.px_60
                ),
                SizedBox(width: PxUtil.instance.px_10,),
                Expanded(child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        Expanded(child: Text(
                          tipListItem?.senderNickname??StringUtil.instance.System_Notion,
                          style: TextStyle(
                              color: ColorUtil.instance.color_white,
                              fontWeight: FontWeight.w500,
                              fontSize: PxUtil.instance.px_16
                          ),
                        ),),
                        // '${TimeUtils.forMatTimeByMs(sessionItem?.lastMessageTime,format: 'yyyy.MM.dd HH:mm:ss')}',
                        Text(
                          '${TimeUtils.forMatTimeByMs(
                              tipListItem?.lastMessageTime??DateTime.now().millisecondsSinceEpoch,format: 'HH:mm')}',
                          style: TextStyle(
                              color: ColorUtil.instance.color_cacaca,
                              fontSize: PxUtil.instance.px_12
                          ),
                        )
                      ],
                    ),
                    Container(
                      margin: EdgeInsets.only(
                          top: PxUtil.instance.px_10
                      ),
                      child: Text(
                        '${tipListItem?.lastMessageContent??'${StringUtil.instance.Welcome_to_Interest_Voice_APP}.......'}',
                        style: TextStyle(
                            color: ColorUtil.instance.color_cacaca,
                            fontSize: PxUtil.instance.px_12
                        ),
                      ),
                    )
                  ],
                ))
              ],
            ),
          ),
        ),
        //对话列表
        ListView.builder(
          shrinkWrap: true,
          padding: EdgeInsets.only(),
          physics: NeverScrollableScrollPhysics(),
          itemCount: model.dataLists?.length??0,
          itemBuilder: (BuildContext context,int index){
            var sessionItem = model.dataLists[index];
            if(((sessionItem?.lastMessageType == NIMMessageType.tip)||
                (sessionItem?.lastMessageType == NIMMessageType.notification)
            ))return SizedBox(width: PxUtil.instance.px_0,);
            UserEntity sessionUserEntity = model.msgPersons.firstWhereOrNull(
                    (element) => (element?.yxAccid==sessionItem?.sessionId));
            return InkWellButton(
              onPressed: ()=>model.toChat(sessionItem,sessionUserEntity),
              child: Container(
                padding: EdgeInsets.only(
                  top: PxUtil.instance.px_8,
                  bottom: PxUtil.instance.px_8,
                ),
                child: Row(
                  children: [
                    Container(
                      width: PxUtil.instance.px_60,
                      height: PxUtil.instance.px_60,
                      child: Stack(
                        children: [
                          CommWidgetManager.buildCicleImagWidget(
                            sessionUserEntity?.icon?? '',
                            placeholder:
                            DrawableUtil.instance.icon_default_head,
                            width: PxUtil.instance.px_60,
                            height: PxUtil.instance.px_60,
                            roundRadius: PxUtil.instance.px_30,
                            backgroundColor: ColorUtil.instance.color_f2f2f2,
                            padding: EdgeInsets.only(),
                            margin: EdgeInsets.only(),
                          ),
                          ((sessionItem?.unreadCount??0)==0)?SizedBox(width: PxUtil.instance.px_0,):
                          Positioned(
                              right: PxUtil.instance.px_0,
                              child:Container(
                                decoration: BoxDecoration(
                                    color: ColorUtil.instance.color_ee0b6b,
                                    borderRadius: BorderRadius.circular(PxUtil.instance.px_20)
                                ),
                                padding: EdgeInsets.only(
                                  left: PxUtil.instance.px_5,
                                  right: PxUtil.instance.px_5,
                                  top: PxUtil.instance.px_3,
                                  bottom: PxUtil.instance.px_3,
                                ),
                                child: Text(
                                  '${(sessionItem?.unreadCount??0)>99?'99+':(sessionItem?.unreadCount??0)}',
                                  style: TextStyle(
                                      color: ColorUtil.instance.color_white,
                                      fontSize: PxUtil.instance.px_10
                                  ),
                                ),
                              )
                          )
                        ],
                      ),
                    ),
                    SizedBox(width: PxUtil.instance.px_10,),
                    Expanded(child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Expanded(child: Text(
                              '${sessionUserEntity?.nickname??''}',
                              style: TextStyle(
                                  color: ColorUtil.instance.color_white,
                                  fontWeight: FontWeight.w500,
                                  fontSize: PxUtil.instance.px_16
                              ),
                            ),),
                            Text(
                              '${TimeUtils.forMatTimeByMs(sessionItem?.lastMessageTime,format: 'yyyy.MM.dd HH:mm:ss')}',
                              style: TextStyle(
                                  color: ColorUtil.instance.color_cacaca,
                                  fontSize: PxUtil.instance.px_12
                              ),
                            )
                          ],
                        ),
                        Container(
                          margin: EdgeInsets.only(
                              top: PxUtil.instance.px_10
                          ),
                          child: Text(
                            '${sessionItem?.lastMessageContent??''}',
                            style: TextStyle(
                                color: ColorUtil.instance.color_cacaca,
                                fontSize: PxUtil.instance.px_12
                            ),
                          ),
                        )
                      ],
                    ))
                  ],
                ),
              ),
            );
          }
        )
      ],
    );
  }
}
